#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@File    :   psqi.py
@Time    :   2020/11/27
@Author  :   wenke wang
@Version :   1.0
@Desc    :   PSQI睡眠评估
'''

# here put the import lib
import sys, os, json
from decimal import Decimal
from psqi_eval_param import PsqiEvalParam

def get_default_settings():
    """
    获取默认的评分配置
    """
    default_setting = None

    setting_path = os.path.join(sys.path[0], "psqi_score_setting.json")
    with open(setting_path, "r", encoding="utf-8") as fp:
        default_setting = json.load(fp)
    
    return default_setting

def evaluate(param, score_settings = None):
    """
    PSQI睡眠评估
    """
    if not score_settings:
        score_settings = get_default_settings()
    
    # A.睡眠质量得分
    score_a = param.sleeping_quality

    # B.入睡时间得分
    #   条目2
    score_fell_asleep = [f["score"] for f in score_settings["fell_asleep"] if f["low"] < param.fell_asleep and f["high"] >= param.fell_asleep][0]
    #   条目5a
    score_not_fell_asleep_30min = param.not_fell_asleep_30min
    #   B得分
    score_b = score_fell_asleep + score_not_fell_asleep_30min
    if score_b > 4 and score_b <= 6:
        score_b = 3
    elif score_b > 2 and score_b <= 4:
        score_b = 2
    elif score_b > 0 and score_b <= 2:
        score_b = 1
    elif score_b <= 0:
        score_b = 0
    
    # C.睡眠时间得分
    if param.fact_sleeping_hours < 5:
        score_c = 3
    elif param.fact_sleeping_hours >= 5 and param.fact_sleeping_hours <= 6:
        score_c = 2
    elif param.fact_sleeping_hours >=6 and param.fact_sleeping_hours <= 7:
        score_c = 1
    else:
        score_c = 0

    # D. 睡眠效率得分
    #   睡眠效率
    score_sleeping_efficiency = (Decimal(param.fact_sleeping_hours) / Decimal(param.on_bed_hours)).quantize(Decimal("0.00"))
    #   D得分
    if score_sleeping_efficiency < Decimal("0.65"):
        score_d = 3
    elif score_sleeping_efficiency >= Decimal("0.65") and score_sleeping_efficiency < Decimal("0.75"):
        score_d = 2
    elif score_sleeping_efficiency >= Decimal("0.75") and score_sleeping_efficiency < Decimal("0.85"):
        score_d = 1
    else:
        score_d = 0
    
    # E.睡眠障碍得分
    score_sleeping_disorders = param.wake_in_night + param.to_restroom_in_night + param.difficult_breathing + param.cough_or_snoring \
        + param.feeling_cold + param.feeling_hot + param.nightmare + param.pain + param.others
    #   E得分
    if score_sleeping_disorders > 18 and score_sleeping_disorders <= 27:
        score_e = 3
    elif score_sleeping_disorders > 9 and score_sleeping_disorders <= 18:
        score_e = 2
    elif score_sleeping_disorders > 0 and score_sleeping_disorders <= 9:
        score_e = 1
    else:
        score_e = 0
    # F.催眠药物得分
    score_f = param.sleeping_pills
    # G.日间功能障碍得分
    score_g = param.sleepy + param.anenergia
    if score_g > 4 and score_g <= 6:
        score_g = 3
    elif score_g > 2 and score_g <= 4:
        score_g = 2
    elif score_g > 0 and score_g <= 2:
        score_g = 1
    else:
        score_g = 0
    
    # PSQI总分
    total_score = score_a + score_b + score_c + score_d + score_e + score_f + score_g

    if total_score <= 5:
        result = "good"
    elif total_score > 5 and total_score <= 10:
        result = "ok"
    elif total_score > 10 and total_score <= 15:
        result = "normal"
    elif total_score > 15 and total_score <= 21:
        result = "bad"
    
    return { "score": total_score, "result": result }

if __name__ == "__main__":
    pass

